using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestDMPropertyNameRule
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region TestDMPropertyNameRule

        [Test]
        public void Test_Contruct()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
        }

        [Test]
        public void Test_Construct_ShouldSupport_IBusinessObjectRule()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Test Result -----------------------
            TestUtilsShared.AssertIsInstanceOf<IBusinessObjectRule>(rule);
        }

        [Test]
        public void Test_Name_ShouldBeCorrect()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            string name = rule.Name;
            //---------------Test Result -----------------------
            Assert.AreEqual("DMPropertyNameRule", name);
        }

        [Test]
        public void Test_Error()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Test Result -----------------------
            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);
        }
        
//        [Test]
//        public void Test_Message()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
//            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
//            dmProperty.Class = dmClass;
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            IBusinessObjectRule rule = new DMPropertyNameRule();
//            //---------------Test Result -----------------------
//            StringAssert.Contains("The Property Name '" + dmProperty.PropertyName + "' on class '" + dmProperty.Class.ToString() + "' is not valid as it is a reserved Property Name.", rule.Message);
//        }
//
//        [Test]
//        public void Test_DMPropertyNameRule_WhenDMPropertyNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMPropertyNameRule();
//                Assert.Fail("expected ArgumentNullException");
//            }
//                //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmProperty", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_IsValid_WhenPropertyNameDirtyXML_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            dmProperty.Class = dmClass;
            dmProperty.PropertyName = "DirtyXML";
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual("DirtyXML", dmProperty.PropertyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Property Name '" + dmProperty.PropertyName + "' on class '" + dmProperty.Class.ToString() +
                "' is not valid as it is a reserved Property Name.", rule.Message);
        } 
           
        [Test]
        public void Test_IsValid_WhenPropertyNameStartsWithDirtyXML_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            dmProperty.PropertyName = "DirtyXMLlog";
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual("DirtyXMLlog", dmProperty.PropertyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_WhenPropertyNameRelationships_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            dmProperty.Class = dmClass;
            dmProperty.PropertyName = "Relationships";
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual("Relationships", dmProperty.PropertyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Property Name '" + dmProperty.PropertyName + "' on class '" + dmProperty.Class.ToString() +
                "' is not valid as it is a reserved Property Name.", rule.Message);
        }

        [Test]
        public void Test_IsValid_WhenPropertyNameProps_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            dmProperty.Class = dmClass;
            dmProperty.PropertyName = "Props";
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual("Props", dmProperty.PropertyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Property Name '" + dmProperty.PropertyName + "' on class '" + dmProperty.Class.ToString() +
                "' is not valid as it is a reserved Property Name.", rule.Message);
        }

        [Test]
        public void Test_IsValid_WhenPropertyNameClassDef_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            dmProperty.Class = dmClass;
            dmProperty.PropertyName = "ClassDef";
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual("ClassDef", dmProperty.PropertyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Property Name '" + dmProperty.PropertyName + "' on class '" + dmProperty.Class.ToString() +
                "' is not valid as it is a reserved Property Name.", rule.Message);
        }

        [Test]
        public void Test_IsValid_WhenPropertyNameStatus_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            dmProperty.Class = dmClass;
            dmProperty.PropertyName = "Status";
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual("Status", dmProperty.PropertyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Property Name '" + dmProperty.PropertyName + "' on class '" + dmProperty.Class.ToString() +
                "' is not valid as it is a reserved Property Name.", rule.Message);
        }
        
        [Test]
        public void Test_IsValid_WhenPropertyNameID_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            dmProperty.Class = dmClass;
            dmProperty.PropertyName = "ID";
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual("ID", dmProperty.PropertyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Property Name '" + dmProperty.PropertyName + "' on class '" + dmProperty.Class.ToString() + 
                "' is not valid as it is a reserved Property Name.", rule.Message);
        }

        [Test]
        public void Test_IsValid_WhenPropertyNameToString_ShouldReturnFalse()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            dmProperty.Class = dmClass;
            dmProperty.PropertyName = "ToString";
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual("ToString",dmProperty.PropertyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        [Test]
        public void Test_IsValid_WhenPropertyNameNotReservedWord_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            dmProperty.PropertyName = TestUtilsShared.GetRandomString();
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_WhenPropertyNameIsNull_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            dmProperty.PropertyName  = null;
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            Assert.IsNull(dmProperty.PropertyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_WhenPropertyNameIsEmpty_ShouldReturnTrue()
        {
            //---------------Set up test pack-------------------
            DMProperty dmProperty = TestUtilsDMProperty.CreateUnsavedDefaultDMProperty();
            dmProperty.PropertyName =  "";
            IBusinessObjectRule rule = new DMPropertyNameRule();
            //---------------Assert Precondition----------------
            Assert.IsNullOrEmpty(dmProperty.PropertyName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmProperty);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        #endregion

    }
}